package com.lyz.myself.controller;

import com.github.pagehelper.PageInfo;
import com.lyz.myself.config.utils.JwtUtil;
import com.lyz.myself.config.utils.Result;
import com.lyz.myself.config.utils.base.CodeMsg;
import com.lyz.myself.config.utils.util.DateUtil;
import com.lyz.myself.pojo.Po.Company;
import com.lyz.myself.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: 机构controller
 * @Date: 2019/1/14 15:56
 * @Author: Mr.Deng
 * @Description:
 */

@Api(description = "机构模块")
@RestController
@RequestMapping("/companyController")
public class CompanyController{
    @Autowired
    private CompanyService companyService;

    @ApiOperation("所有机构信息")
    @RequestMapping(path = "/selectAllCompany", method = RequestMethod.GET)
    public Result selectAllCompany(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Company companyTemp = companyService.selectByPrimaryKey(0);
        if (null == companyTemp) {
            Company company = new Company();
            company.setCompany_id(0);
            company.setFull_name("祺业电商");
            companyService.insertSelective(company);
        }
        PageInfo<Map<String, String>> pageInfo = companyService.selectAllCompany(pageNum, pageSize);
        if (pageInfo.getSize() > 0) {
            return Result.success(pageInfo, "机构信息查询成功！");
        } else {
            return Result.error(CodeMsg.ERROR, "暂无机构信息！");
        }
    }

    @ApiOperation("添加机构信息")
    @RequiresAuthentication//登陆状态
    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @RequestMapping(path = "/insertCompany", method = RequestMethod.POST)
    public Result insertCompany(@RequestBody Company company) {
        if (company != null) {
            boolean insertCompany = companyService.insertSelective(company);
            if (insertCompany == true) {
                return Result.success("添加成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "添加失败！");
            }
        } else {
            return Result.error(CodeMsg.ERROR, "请输入要添加的机构信息！");
        }
    }

    @ApiOperation("修改机构信息")
    @RequiresAuthentication//登陆状态
    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @RequestMapping(path = "/updateCompany", method = RequestMethod.POST)
    public Result updateCompany(@RequestBody Company company) {
        if (company != null) {
            boolean insertCompany = companyService.updateByPrimaryKeySelective(company);
            if (insertCompany == true) {
                return Result.success("修改成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "修改失败！");
            }
        } else {
            return Result.error(CodeMsg.ERROR, "请输入要修改的机构信息！");
        }
    }

    @ApiOperation("删除机构信息")
    @RequiresAuthentication//登陆状态
    @RequiresRoles(value = {"admin"}, logical = Logical.OR)
    @RequestMapping(path = "/deleteCompany", method = RequestMethod.POST)
    public Result deleteCompany(@RequestParam("company_id") String company_id) {
        if (company_id != null) {
            boolean insertCompany = companyService.deleteCompany(company_id);
            if (insertCompany == true) {
                return Result.success("删除成功！");
            } else {
                return Result.error(CodeMsg.ERROR, "删除失败！");
            }
        } else {
            return Result.error(CodeMsg.ERROR, "请输入要删除的机构id！");
        }
    }


}
